package top.psjj.news.web;

import org.apache.commons.beanutils.BeanUtils;
import top.psjj.news.po.TNews;
import top.psjj.news.po.TType;
import top.psjj.news.service.NewsService;
import top.psjj.news.service.TypeService;
import top.psjj.news.service.impl.NewsServiceImpl;
import top.psjj.news.service.impl.TypeServiceImpl;
import top.psjj.news.utils.ServiceProxyFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @Auther:小陈的Java
 */
@WebServlet("/news")
public class NewsServlet extends BasicServlet {
    private TypeService typeService = (TypeService) ServiceProxyFactory.getService((TypeServiceImpl.class));
    private NewsService newsService = (NewsService) ServiceProxyFactory.getService((NewsServiceImpl.class));
    private String mainUrl = "/WEB-INF/jsp/main/mainTemp.jsp";
    private String baseUrl = "./news/";

    public void addUI(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        List<TType> types = typeService.queryAll();
        req.setAttribute("types",types);
        req.setAttribute("mainPage",baseUrl+"newsSave.jsp");
        req.getRequestDispatcher(mainUrl).forward(req,resp);
    }
    public void save(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String sid =req.getParameter("id");
        String title = req.getParameter("title");
        String author = req.getParameter("author");
//
        if(sid == null || "".equals(sid)){
            Map<String,String[]> parameterMap =req.getParameterMap();
            TNews news = new TNews();
            BeanUtils.populate(news,parameterMap);
            newsService.add(news);
        }else{
            int id = Integer.parseInt(sid);
            TNews tNews = new TNews();
            tNews.setTitle(title);
            tNews.setAuthor(author);
//
            tNews.setId(id);
            newsService.modify(tNews);
        }
        resp.sendRedirect(req.getContextPath()+"/news?method=list");
    }
    public void list(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        String title = req.getParameter("s_title");
        String s_bPublishDate = req.getParameter("s_bPublishDate");
        String s_aPublishDate = req.getParameter("s_aPublishDate");
        StringBuffer sql = new StringBuffer();
        sql.append("select id,title,author,type_id typeId,isHead ishead,isImg isimg,isHot ishot,content,public_date publicDate,click,img_url imgUrl from t_news ");
        sql.append(" where 1=1 ");
        if(title!=null && !("".equals(title))){
            sql.append("and title like '%"+title+"%' ");
        }
        if(s_bPublishDate!=null && !("".equals(s_bPublishDate))){
            sql.append("and public_date > '"+s_bPublishDate+"' ");
        }
        if(s_aPublishDate!=null && !("".equals(s_aPublishDate))){
            sql.append("and public_date < '"+s_aPublishDate+"' ");
        }

        sql.append(" order by public_date desc");

        List<TNews> list = newsService.list(sql.toString());
        req.setAttribute("list",list);
        //System.out.println(list);
        req.setAttribute("mainPage",baseUrl+"newsList.jsp");
        req.getRequestDispatcher(mainUrl).forward(req,resp);
    }
    public void del(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        int id = Integer.parseInt(req.getParameter("id"));
        newsService.del(id);
        resp.sendRedirect(req.getContextPath()+"/news?method=list");
    }
    public void updateUI(HttpServletRequest req,HttpServletResponse resp) throws Exception{
        int id = Integer.parseInt(req.getParameter("id"));
        TNews tNews= newsService.queryTNewsById(id);
        final List<TType> types = typeService.queryAll();
        req.setAttribute("news",tNews);
        req.setAttribute("mainPage",baseUrl+"newsSave.jsp");
        req.setAttribute("types",types);
        req.getRequestDispatcher(mainUrl).forward(req,resp);
    }
}
